package dao;

import java.util.List;

import model.Reviews;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import idao.IReviewsDAO;

@Repository
public class ReviewsDAO implements IReviewsDAO {

	@Autowired
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public ReviewsDAO() {
		// TODO Auto-generated constructor stub
	}

	
	public boolean insert(model.Reviews reviews) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().save(reviews);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	
	public boolean update(model.Reviews reviews) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().update(reviews);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	
	public boolean delete(model.Reviews reviews) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().delete(reviews);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	
	public Reviews getReviewByID(int id) {
		// TODO Auto-generated method stub
		try {
			Reviews reviews = (Reviews) sessionFactory.getCurrentSession().get(Reviews.class, id);
			return reviews;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	
	@SuppressWarnings("unchecked")
	public List<model.Reviews> getAll() {
		// TODO Auto-generated method stub
		try {
			Query query = sessionFactory.getCurrentSession().createQuery("from Reviews");
			return query.list();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

}
